package com.swy.shopgroup.controller;

import com.swy.shopgroup.ResultUtils;
import com.swy.shopgroup.UploadFilePathConfig;
import com.swy.shopgroup.dao.ShopInfoMapper;
import com.swy.shopgroup.entity.Goods;
import com.swy.shopgroup.entity.Mact;
import com.swy.shopgroup.entity.Result;
import com.swy.shopgroup.entity.ShopInfo;
import com.swy.shopgroup.service.GoodsService;
import com.swy.shopgroup.service.MactService;
import com.swy.shopgroup.service.ShopService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("/ShopService")
public class ShopServiceController {

    @Resource
    private GoodsService goodsService;
    @Resource
    private MactService mactService;

    @RequestMapping(value = "/insertGoods")
    @ResponseBody
    public Result insertGoods(@RequestParam("file") MultipartFile file, Goods goods) throws IOException {
        String path = UploadFilePathConfig.uploadPath;
        // 上传图片
        if (file != null && !file.isEmpty()) {
            // 获取文件的名称
            String oldname = file.getOriginalFilename();
            // 获取文件的后缀名
            String sux = oldname.substring(oldname.lastIndexOf("."));
            // 自动生成文件名
            String newname = UUID.randomUUID().toString() + sux;
            // 获取文件的上传路径（物理路径）
            String upload = path + newname;
            System.out.println(upload);
            // 上传图片
            file.transferTo(new File(upload));
            goods.setIcon(newname);
        }
        return goodsService.insertGoods(goods);
    }

    @RequestMapping(value = "/insertMact")
    @ResponseBody
    public Result insertMact(@RequestParam("file") MultipartFile file, Mact mact) throws IOException {
        String path = UploadFilePathConfig.uploadPath;
        // 上传图片
        if (file != null && !file.isEmpty()) {
            // 获取文件的名称
            String oldname = file.getOriginalFilename();
            // 获取文件的后缀名
            String sux = oldname.substring(oldname.lastIndexOf("."));
            // 自动生成文件名
            String newname = UUID.randomUUID().toString() + sux;
            // 获取文件的上传路径（物理路径）
            String upload = path + newname;
            System.out.println(upload);
            // 上传图片
            file.transferTo(new File(upload));
            mact.setIcon(newname);
        }
        return mactService.insertAct(mact);
    }

    @RequestMapping(value = "/selectGoods")
    @ResponseBody
    public Result selectGoods() {
        return goodsService.selectByState();
    }

    @RequestMapping(value = "/selectAct")
    @ResponseBody
    public Result selectAct() {
        return mactService.selectByState();
    }

    @RequestMapping(value = "/selectShopGoods")
    @ResponseBody
    public Result selectShopGoods(Long id) {
        return goodsService.selectByShop(id);
    }

    @RequestMapping(value = "/selectShopAct")
    @ResponseBody
    public Result selectShopAct(Long id) {
        return mactService.selectByShop(id);
    }

}
